package com.linkhub.oms.data.sync.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.dtflys.forest.http.ForestResponse;
import com.linkhub.oms.common.enums.mp.CurrencyTypeEnum;
import com.linkhub.oms.dao.mpservice.IExchangeRateService;
import com.linkhub.oms.data.sync.api.account.ExchangeRateClient;
import com.linkhub.oms.data.sync.api.account.domain.response.ExchangeRateResponse;
import com.linkhub.oms.data.sync.service.ExchangeRateApiService;
import com.linkhub.oms.entity.ExchangeRateEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.linkhub.oms.common.constant.ProjectConstant.ASYNC_EXECUTOR;

/**
 * ExchangeRateApiServiceImpl
 *
 * @author Frank
 */
@Slf4j
@Service
public class ExchangeRateApiServiceImpl implements ExchangeRateApiService {
    @Resource
    private ExchangeRateClient exchangeRateClient;
    @Resource
    private IExchangeRateService iExchangeRateService;

    @Override
    @Async(ASYNC_EXECUTOR)
    @Transactional(rollbackFor = Exception.class)
    public void synExchangeRates() {
        LocalDate now = LocalDate.now();
        List<ExchangeRateEntity> exchangeRateEntities = iExchangeRateService.listExchangeRate(now);
        ExchangeRateEntity exchangeRateEntity = null;
        if (CollUtil.isNotEmpty(exchangeRateEntities)) {
            exchangeRateEntity = exchangeRateEntities.get(0);
            if (Objects.nonNull(exchangeRateEntity)) {
                Map<String, String> exchangeRate = exchangeRateEntity.getExchangeRate();
                if (MapUtil.isNotEmpty(exchangeRate) && exchangeRate.size() >= CurrencyTypeEnum.values().length) {
                    return;
                }
            }
        }
        ForestResponse<ExchangeRateResponse> response;
        try {
            response = exchangeRateClient.listExchangeRates();
        } catch (Exception e) {
            log.error("同步汇率失败", e);
            return;
        }
        ExchangeRateResponse result = response.getResult();
        if (Objects.isNull(result)) {
            log.info("暂无汇率数据");
            return;
        }
        if (!result.isSuccess()) {
            log.info("汇率接口返回失败");
            return;
        }
        Map<String, String> conversionRates = result.getConversionRates();
        if (MapUtil.isEmpty(conversionRates)) {
            log.info("暂无汇率数据");
            return;
        }
        Map<String, String> resultMap = MapUtil.newHashMap();

        for (CurrencyTypeEnum currencyTypeEnum : CurrencyTypeEnum.values()) {
            String rate = conversionRates.get(currencyTypeEnum.getTypeStr());
            if (StringUtils.isNotBlank(rate)) {
                resultMap.put(currencyTypeEnum.getTypeStr(), rate);
            }
        }

        if (Objects.isNull(exchangeRateEntity)) {
            ExchangeRateEntity entity = new ExchangeRateEntity();
            entity.setExchangeRate(resultMap);
            entity.setRateDate(now);
            iExchangeRateService.save(entity);
        } else {
            exchangeRateEntity.setExchangeRate(resultMap);
            iExchangeRateService.updateById(exchangeRateEntity);
        }

    }

}
